<?php
namespace app\admin\controller;

use controller\BasicAdmin;
use service\DataService;
use think\Db;

/**
 * 前台单页文章控制器
 *
 * @author leo.lei(346991581@qq.com)
 */
class Page extends BasicAdmin
{
    /**
     * 列表
     */
    public function index()
    {
        //获取类型
        $title = '单页列表';
        //获取列表
        $list = Db::name('YogaCategory')->where(['type'=>1,'is_deleted'=>'0','status'=>'1'])->select();
        $this->assign('title', $title);
        $this->assign('list', $list);
        return view();
    }

    /**
     * 编辑
     */
    public function edit()
    {
        $id = $this->request->get('id', '');
        if ($this->request->isGet()) {
            empty($id) && $this->error('参数错误，请稍候再试！');
            $this->assign('title', '编辑单页文章');
            $info = Db::name('YogaArticle')->where('cid', $id)->find();
            if ($info) {
                $this->assign('vo', $info);
            } else {
                $this->assign('vo', ['title'=>$this->request->get('title'),'cid'=>$id,'type'=>1,'desc'=>null,'content'=>null]);
            }
            return view('form');
        }
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if (!isset($data['id'])) {
                $data['create_time'] = NOW_TIME;
            }
            $data['update_time'] = NOW_TIME;
            if (DataService::save('YogaArticle', $data, 'id') !== false) {
                $this->success('单页更新成功！', '');
            }
            $this->error('单页更新失败，请稍候再试！');
        }
    }
}
